package com.ndbg.demo.service.resource.impl;

import com.ndbg.demo.service.resource.IResourceService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

@Service
public class ResourceServiceImpl implements IResourceService, ApplicationContextAware, ResourceLoaderAware {

    private ApplicationContext applicationContext;

    private ResourceLoader resourceLoader;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void resourceTest() {
        Resource resource = resourceLoader.getResource("classpath:test.xlsx");
        try {
            resource.getURI();
            InputStream inputStream = resource.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
}
